
package com.yatu.codegen;

import java.sql.SQLException;
import java.util.List;

import com.yatu.codegen.db.DBService;
import com.yatu.codegen.db.DBServiceImpl;
import com.yatu.codegen.execute.CodeMeachine;
import com.yatu.codegen.util.ConvertUtil;
import com.yatu.codegen.util.PropertiesUtil;

public final class RunEntrance {

	private DBService dbService = new DBServiceImpl();
	private String database;
	private String[] excludes;
	private String[] scopes = PropertiesUtil.load("codegen.scope", "").split(",");
	private String generatePath;
	private String packageName;
	private static String serviceName = PropertiesUtil.load("service.name", "service");
	private static String repositoryName = PropertiesUtil.load("repository.name", "dao");
	private static String webName = PropertiesUtil.load("web.name", "controller");

	public RunEntrance(String database, String[] excludes, String generatePath, String packageName) {
		this.database = database;
		this.excludes = (excludes == null ? new String[] {} : excludes);
		this.generatePath = generatePath;
		this.packageName = packageName;
	}

	private String getClass4Table(String table) {
		return ConvertUtil.format(ConvertUtil.convert(table));
	}

	private String getClass4Table(String table, String suffix) {
		return getClass4Table(table) + (suffix == null ? "" : ConvertUtil.format(suffix));
	}

	public void run() {
		List<String> tables = dbService.getTables(database, excludes);
		CodeMeachine.setGeneratePath(generatePath);
		CodeMeachine.setPackageName(packageName);
		// service,dao,domain,controller,serviceImpl,daoImpl

		try {
			for (String table : tables) {
				for (String scope : scopes) {
					if (scope.toUpperCase().equals("service".toUpperCase())) {
						CodeMeachine.generateFile("ServiceInterface.ftl", table,
								this.getClass4Table(table, serviceName) + ".java");
					} else if (scope.toUpperCase().equals("dao".toUpperCase())) {

					} else if (scope.toUpperCase().equals("domain".toUpperCase())) {
						CodeMeachine.generateFile("poModel.ftl", table,
								ConvertUtil.format(ConvertUtil.convert(table)) + ".java");
					} else if (scope.toUpperCase().equals("controller".toUpperCase())) {
						CodeMeachine.generateFile("Action.ftl", table, this.getClass4Table(table, webName) + ".java");
					} else if (scope.toUpperCase().equals("serviceImpl".toUpperCase())) {
						CodeMeachine.generateFile("ServiceImpl.ftl", table,
								this.getClass4Table(table, serviceName + "Impl") + ".java");
					} else if (scope.toUpperCase().equals("daoImpl".toUpperCase())) {

					} else if (scope.toUpperCase().equals("jsp".toUpperCase())) {
						CodeMeachine.generateFile("jDeptListPage.jsp", table, "list.jsp");
						CodeMeachine.generateFile("jDeptCreate.jsp", table, "add.jsp");
						CodeMeachine.generateFile("jDeptUpdate.jsp", table, "edit.jsp");
						CodeMeachine.generateFile("jDeptView.jsp", table, "ui.jsp");
					}
				}
			}
		} catch (SQLException e) {
		}
		System.out.println("Generate success.");
	}
}
